package com.huitone.smspfm.socket.protocol.sgip.message.msgbody;

import java.io.UnsupportedEncodingException;

import com.bin.common.utils.ArrayUtils;
import com.huitone.smspfm.socket.protocol.sgip.constant.Constant;
import com.huitone.smspfm.socket.protocol.sgip.conversion.TransferConverter;

/**   
* @Title: Deliver.java 
* @Package com.huitone.smspfm.socket.protocol.sgip.msgbody 
* @Description: TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年3月23日 上午11:05:49 
* @version V1.0   
*/
public class Deliver implements IBody {

	/**
	 * @字节长度 21
	 * @数据类型 Text
	 * @DESC
	 *  业务端编码
	 */
	private byte[] srvNode = new byte[21];
	
	
	/**
	 * @字节长度 21
	 * @数据类型 Text / 字符串都是由数字组成
	 * @DESC
	 *  发送端口号
	 */
	private byte[] sendPort = new byte[21];
	
	/**
	 * @字节长度 21
	 * @数据类型 Text
	 * @DESC
	 *  发送短消息的用户手机号，手机号码前加“86”国别标志
	 */
	private byte[] userNumber = new byte[13];
	
	/**
	 * @字节长度 21
	 * @数据类型 Text / yyyyMMddHHmmss
	 * @DESC
	 *  发送端口号
	 */
	private byte[] recvTime = new byte[14];
	
	/**
	 * @字节长度 1
	 * @数据类型 Integer
	 * @DESC
	 *  短消息的编码格式。
		0：纯ASCII字符串
		3：写卡操作
		4：二进制编码
		8：UCS2编码
		15: GBK编码
		其它参见GSM3.38第4节：SMS Data Coding Scheme
	 */
	private byte[] msgCoding = new byte[1];
	
	/**
	 * @字节长度 4
	 * @数据类型 Integer
	 * @DESC
	 *  短消息的长度
	 */
	private byte[] msgLength = new byte[4];
	
	/**
	 * @字节长度 messageLength
	 * @数据类型 Text
	 * @DESC
	 *  短消息的内容
	 */
	private byte[] msgContent;
	
	public static Deliver build(byte[] bytes) {
		
		Deliver deliver = new Deliver();
		
		int index = 0;
		
		deliver.srvNode = ArrayUtils.subarray(bytes, index, index += 21);
		deliver.sendPort = ArrayUtils.subarray(bytes, index, index += 21);
		deliver.userNumber = ArrayUtils.subarray(bytes, index, index += 13);
		deliver.recvTime = ArrayUtils.subarray(bytes, index, index += 14);
		deliver.msgCoding = ArrayUtils.subarray(bytes, index, index += 1);
		deliver.msgLength = ArrayUtils.subarray(bytes, index, index += 4);
		deliver.msgContent = ArrayUtils.subarray(bytes, index, index += deliver.getMsgLength());
		
		return deliver;
	}
	
	public static Deliver build(String srvNode, String sendPort, String userNumber, String recvTime, int msgCoding, String msgContent) {
		
		Deliver deliver = new Deliver();
		
		deliver.setSrvNode(srvNode);
		deliver.setSendPort(sendPort);
		deliver.setUserNumber(userNumber);
		deliver.setRecvTime(recvTime);
		deliver.setMsgCoding(msgCoding);
		deliver.setMsgContent(msgContent);
		
		return deliver;
	}

	public String getUserNumber() {
		return TransferConverter.lj2Str(userNumber);
	}

	public void setUserNumber(String userNumber) {
		this.userNumber = TransferConverter.toLJ(this.userNumber, userNumber);
	}

	public String getSrvNode() {
		return TransferConverter.lj2Str(srvNode);
	}

	public void setSrvNode(String srvNode) {
		this.srvNode = TransferConverter.toLJ(this.srvNode, srvNode);
	}

	public int getMsgCoding() {
		return TransferConverter.bE2Int1(this.msgCoding);
	}

	public void setMsgCoding(int msgCoding) {
		this.msgCoding = TransferConverter.toBE1(msgCoding);
	}

	public int getMsgLength() {
		return TransferConverter.bE2Int4(this.msgLength);
	}

	public void setMsgLength(int msgLength) {
		this.msgLength = TransferConverter.toBE4(msgLength);
	}

	public String getMsgContent() {
		String result = null;
		try {
			result = new String(this.msgContent, Constant.MessageCodingP.parse(getMsgCoding()));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	public void setMsgContent(String msgContent) {
		
		if (msgContent != null) {
			
			byte[] bytes;
			try {
				bytes = msgContent.getBytes(Constant.MessageCodingP.parse(getMsgCoding()));
				setMsgLength(bytes.length);
				this.msgContent = bytes;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
	}

	public String getSendPort() {
		return TransferConverter.lj2Str(sendPort);
	}

	public void setSendPort(String sendPort) {
		this.sendPort = TransferConverter.toLJ(this.sendPort, sendPort);
	}
	
	public String getRecvTime() {
		return TransferConverter.lj2Str(recvTime);
	}

	public void setRecvTime(String recvTime) {
		this.recvTime = TransferConverter.toLJ(this.recvTime, recvTime);
	}

	@Override
	public byte[] toByteArray() {
		return ArrayUtils.addAll(srvNode, sendPort, userNumber, recvTime, msgCoding, msgLength, msgContent);
	}

	@Override
	public int length() {
		return toByteArray().length;
	}

	@Override
	public String toString() {
		return "Deliver [业务端号=" + getSrvNode() + ", 用户号码=" + getUserNumber() + ", 短信内容=" + getMsgContent() + ", 短信编码="
				+ getMsgCoding() + ", 扩展码=" + getSendPort() + ", 接收时间=" + getRecvTime() + "]";
	}

	@Override
	public String toSimpleString() {
		return getUserNumber() + " " + getSrvNode() + " " + getSendPort() + " " + getRecvTime();
	}
}
